<?php

include_once('top.php');
$project = mysql_real_escape_string($_GET['project']);

if (!empty($_POST)) {
	$name = mysql_real_escape_string($_POST['name']);
	$description = mysql_real_escape_string($_POST['description']);
	$date = date('Y-m-d', strtotime($_POST['date']));
	$assignee = mysql_real_escape_string($_POST['assignee']);
	$milestone = mysql_real_escape_string($_GET['milestone']);
	
	$open = '1';
	
	if(isset($_POST['closed'])) {
		$open = '0';
	}
	
	if (empty($name) || empty($_POST['date']) || empty($assignee)) {
		$error = 'All fields (except description) are required.';
	} else {
		$m = fetch_only("SELECT `Due_date` FROM `MILESTONE` WHERE `Name`='$milestone' AND `Project`='$project'");
		
		if (strtotime($m['Due_date']) >= strtotime($_POST['date'])) {
			run("INSERT INTO `TASK` (`Name`, `Milestone`, `Project`, `Description`, `Due_date`, `Employee`, `Open`) VALUES ('$name', '$milestone', '$project', '$description', '$date', '$assignee', '$open')");
			header("location: task_info.php?name=$name&milestone=$milestone&project=$project");
		} else {
			$error = 'Invalid due date (the milestone is due on ' . date('m/d/Y', strtotime($m['Due_date'])) . ')';
		}
	}
}

$users = fetch_all("SELECT `First_name`, `Last_name`, `Email` FROM `USER`, `PROJECT_WORKERS` WHERE `PROJECT_WORKERS`.`Employee` = `USER`.`Email` AND `PROJECT_WORKERS`.`Project`='$project'");
$html = '';

for ($i = 0; $i < count($users); $i++) {
	$name = "{$users[$i]['First_name']} {$users[$i]['Last_name']}";
	$html .= '<option value="' . $users[$i]['Email'] . '">' . $name . '</option>';
}

?>

<html>
<head>
	<title>GT Project Manager - Task Info</title>
	<link rel="stylesheet" type="text/css" href="main.css" />
	<link rel="stylesheet" type="text/css" href="inner.css" />
</head>
<body>
	<form action="#" method="post">
		<table cellspacing="0" cellpadding="0" id="main">
			<tr><td id="title">
				New Task
				<div class="nav">
					<a href="login.php?action=logout">Log Out</a><br />
					<?php echo $link ?><br />
					<a href="milestone_info.php?name=<?php echo $_GET['milestone'] ?>&project=<?php echo $_GET['project'] ?>">Milestone Info</a>
				</div>
			</td></tr>
			<?php
			if ($error) {
			?>
			<tr><td class="message" colspan="2"><?php echo $error ?></td></tr>
			<?php
			}
			?>
			<tr>
				<td>
					<table class="details" cellspacing="0" cellpadding="0">
						<tr>
							<th>Milestone</th>
							<td><?php echo $_GET['milestone'] ?></td>
						</tr>
						<tr>
							<th>Name</th>
							<td>
								<input type="text" name="name"
									value="" />
							</td>
						</tr>
						<tr>
							<th>Description</th>
							<td>
								<textarea type="text" name="description" resizable="false"></textarea>
							</td>
						</tr>
						<tr>
							<th>Due Date</th>
							<td class="date">
								<input type="text" name="date" value="" />
							</td>
						</tr>
						<tr>
							<th>Assigned To</th>
							<td>
								<select name="assignee">
									<?php echo $html ?>
								</select>
							</td>
						</tr>
						<tr>
							<th>Closed?</th>
							<td><input type="checkbox" name="closed" /></td>
						</tr>
					</table>
				</td>
			</tr>
			<tr>
				<td class="button_bar">
					<input type="submit" value="Save" />
				</td>
			</tr>
		</table>
	</form>
</body>
</html>
